package com.etone;

import java.lang.management.*;
import java.io.*;

/**
 * statistics the memory objects
 * @author Administrator
 *
 */
public class ObjectStatistics {
    public static void main(String[] args) {
        try {
            System.out.println(ManagementFactory.getRuntimeMXBean().getName());
            String pid = ManagementFactory.getRuntimeMXBean().getName().replaceAll("(\\d+)@.*", "$1");
            String cmd = "jmap -histo " + pid;
            System.out.println(cmd);
            final Process p = Runtime.getRuntime().exec(cmd);
            Thread t = new Thread() {
                public void run() {
                    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String buf;
                    try {
                        while ((buf=br.readLine()) != null) {
                            System.out.println(buf);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
